package com.mysiteforme.admin.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.mysiteforme.admin.entity.RfidList;
import com.mysiteforme.admin.service.rfid.RfidListService;
import com.mysiteforme.admin.service.rfid.RfidPottingService;
import com.mysiteforme.admin.util.other.LayerData;
import com.mysiteforme.admin.annotation.SysLog;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * <p>
 * RFID表  前端控制器
 * </p>
 *
 * @author rober
 * @since 2020-07-28
 */
@Controller
public class BaseUnitController {
    private static final Logger LOGGER = LoggerFactory.getLogger(BaseUnitController.class);
    @Value("${excelPath}")
    private String excelPath;
    
	@GetMapping("scrach/download")
    @SysLog("跳转potting/download表列表")
    public void download(HttpServletRequest request, HttpServletResponse response,String fileName) throws UnsupportedEncodingException, FileNotFoundException{  

     String filePath = excelPath+fileName;
//		final String userAgent = request.getHeader("USER-AGENT");
//	    if (StringUtils.contains(userAgent, "MSIE")
//	    		|| StringUtils.contains(userAgent, "Trident")
//	    		|| StringUtils.contains(userAgent, "Edge")) {//IE浏览器
//	    	   filePath = URLEncoder.encode(filePath, "UTF-8");
//	    } else if (StringUtils.contains(userAgent, "Mozilla")) {//google,火狐浏览器
//	    	filePath = new String(filePath.getBytes(), "ISO8859-1");
//	    } else {
//	    	filePath = URLEncoder.encode(filePath, "UTF-8");//其他浏览器
//	    }
		response.addHeader("content-Type","application/octet-stream" );
		response.addHeader("content-Disposition","attachement;filename="+fileName);
			InputStream in =new FileInputStream(filePath) ;
			try {
			ServletOutputStream out = response.getOutputStream() ;	
			byte[] bs = new byte[10];
			int len=-1 ;
			while(  (len=in.read(bs)) != -1) {
				out.write(bs,0,len);
			}
			out.close();
			in.close();
			} catch (IOException e) {	
				// TODO Auto-generated catch block	
		        e.printStackTrace();	
			}
	    }


}